const query = require('../utils/DB')
class goodsModel {
	// 获取商品列表
	static async getGoodsList(
		nowPage = 1,
		pageSize = 10
	) {
		let start = (nowPage - 1) * pageSize
		let listCount = await query(`select count(*) as num from goods`)
		let list = await query(
			`select a.*,b.name as cname from goods as a,category as b where a.cid=b.id limit ${start},${pageSize}`)

		if (list.length > 0) {
			for (let i = 0; i < list.length; i++) {
				list[i].sku = await query(`select * from sku where gid=${list[i].id}`)
				list[i].tag = await query(`select * from tag where gid=${list[i].id}`)
			}
		}
		return {
			count: listCount[0].num,
			list: list
		}
	}
	// 添加商品
	static addGoods(img, price, name, concats, tell, detail, guize, tuigai, address, lng, lat, status, cid,position) {
	// 	console.log(img, price, name, concats, tell, detail, guize, tuigai, address, lng, lat, status, cid,26)
	// console.log(`INSERT INTO goods(img, price, name, concats, tell, detail, guize, tuigai, address, lng, lat, status, cid) VALUES ('${img}', ${price}, '${name}', '${concats}', '${tell}', '${detail}', '${guize}', '${tuigai}', '${address}', '${lng}', '${lat}', ${status}, ${cid})
// `)
		return query(`INSERT INTO goods(img, price, name, concats, tell, detail, guize, tuigai, address, lng, lat, status, cid,position) VALUES ('${img}', ${price}, '${name}', '${concats}', '${tell}', '${detail}', '${guize}', '${tuigai}', '${address}', '${lng}', '${lat}', ${status}, ${cid},'${position}')
`)
	}

	// 修改商品
	static editGoods(id, img, price, name, concats, tell, detail, guize, tuigai, address, lng, lat, status, cid,position) {
		return query(
			`UPDATE goods SET img = '${img}', price = ${price}, name = '${name}', concats = '${concats}', tell = '${tell}', detail = '${detail}', guize = '${guize}', tuigai = '${tuigai}', address = '${address}', lng = '${lng}', lat = '${lat}', status = ${status}, cid = ${cid},position='${position}' WHERE id = ${id}`
			)
	}

	// 删除商品
	static async delGoods(id) {
		// 规格 删除
		await query(`delete from sku where gid=${id}`)
		//  tag 删除
		await query(`delete from tag where gid=${id}`)
		// 商品 删除
		return await query(`delete from goods where id=${id}`)
	}
	
	// // 获取sku
	// static getSku(gid){
	// 	return query(`select * from sku where gid = ${gid}`)
	// }
	// 添加sku
	static addSku(gid, name, miaoshu, price, num, kaishi, jieshu){
		return query(`INSERT INTO sku(id, gid, name, miaoshu, price, num, kaishi, jieshu) VALUES (NULL, ${gid}, '${name}', '${miaoshu}', ${price}, ${num},' ${kaishi}', '${jieshu}')`)
	}

// // 修改sku
	static editSku(id,gid, name, miaoshu, price, num, kaishi, jieshu){
		return query(`UPDATE  sku SET gid = ${gid}, name = '${name}', miaoshu = '${miaoshu}', price = ${price}, num = ${num}, kaishi = ${kaishi}, jieshu = ${jieshu} WHERE id = ${id}`)
	}
	// 删除sku
	static delSku(id){
		return query(`delete from sku where id= ${id}`)
	}
	
	// 添加tag
	static addTag(name, gid, color){
		return query(`INSERT INTO tag(name, gid, color) VALUES ('${name}', ${gid}, '${color}')`)
	}
	// 修改tag
	static editTag(id,name, gid, color){
		return query(`UPDATE tag SET name = '${name}', gid = ${gid}, color = '${color}' WHERE id = ${id}`)
	}
	// 删除tag
	static delTag(id){
		return query(`delete from tag where id= ${id}`)
	}
}
module.exports = goodsModel